import 'dart:async';
import 'package:flutter/material.dart';

class ThemeDataBloc {
  StreamController<ThemeData> _streamController;
  Stream<ThemeData> _stream;
  ThemeData _themeData;
  //这边使用单例模式，保证每次获得同一个实例化对象
  static ThemeDataBloc _instance;

  ThemeDataBloc._insteral() {
    _streamController = StreamController();
    _stream = _streamController.stream;
    _themeData = ThemeData.light();
  }

  static ThemeDataBloc _getInstance() {
    if (_instance == null) {
      _instance = ThemeDataBloc._insteral();
    }
    return _instance;
  }

  factory ThemeDataBloc() => _getInstance();
  static ThemeDataBloc get instance=>_getInstance();

  Stream<ThemeData> get stream => _stream;

  changeTheme() {
    _themeData =
        _themeData == ThemeData.light() ? ThemeData.dark() : ThemeData.light();
    _streamController.sink.add(_themeData);
  }

  dispose() {
    _streamController.close();
  }
}
